package com.yehui.algorithm.sort.impl;

import com.yehui.algorithm.sort.IntArraySort;

/**
 * 希尔排序
 */
public class ShellSort implements IntArraySort {

    @Override
    public void sort(int[] source){
        int gap = 0;
        while(gap < source.length / 3) {
            gap = gap * 3 + 1;
        }
        int j = 0;
        for (; gap > 0; gap /= 3) {
            for (int i = gap; i < source.length; i++) {
                int temp = source[i];
                for (j = i - gap; j >= 0 && temp < source[j]; j = j - gap) {
                    source[j + gap] = source[j];
                }
                source[j + gap] = temp;
            }
        }
    }
}
